﻿using AutoMapper;
using HamcoWcs.Contract.ContractLcd;
using HamcoWcs.DAL;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.LcdInfos;
using HamcoWcs.Service.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace HamcoWcs.Service.Lcd
{
    [AutoInject(typeof(ILcdService), InjectType.Single)]
    public class LcdService : ILcdService
    {
        private readonly IMapper _mapper;
        private readonly IServiceScopeFactory _scopeFactory;
        private WcsDbContext _wcsDbContext => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<WcsDbContext>();


        
        public LcdService(IMapper mapper, IServiceScopeFactory scopeFactory)
        {
            LcdDic = new Dictionary<string, LcdInfoDto>();
            _mapper = mapper;
            _scopeFactory = scopeFactory;
            using (var context = _wcsDbContext)
            {
                var lcdInfos = context.LcdInfos.ToList();
                var lcdInfoDtos = _mapper.Map<List<LcdInfoDto>>(lcdInfos);
                lcdInfoDtos.ForEach(d =>
                {
                    LcdDic.TryAdd(d.LcdName, d);
                });
            }

            if (LcdDic != null)
            {
                foreach (var key in LcdDic.Keys)
                {
                    var msgList = new List<LcdMsg>()
                    {
                        new LcdMsg() {Key="名称" ,Value=key},
                        new LcdMsg() {Key="工作模式" ,Value=""},
                        //new LcdMsg() {Key="预留单号" ,Value=""},
                        //new LcdMsg() {Key="剩余数量" ,Value=""},
                        //new LcdMsg() {Key="包材规格" ,Value="45 | 65 | 75 | 85"},
                        //new LcdMsg() {Key="包材数量" ,Value=""},
                        //new LcdMsg() {Key="包材水位" ,Value=""},
                        //new LcdMsg() {Key="状态" ,Value=""},
                    };
                    LcdDic[key].Msgs = msgList;
                }
            }

        }

        public Dictionary<string, LcdInfoDto> LcdDic { get; set; }

        public List<LcdInfoDto> GetLcdInfo() => LcdDic?.Values.ToList() ?? new List<LcdInfoDto>();

        public List<LcdMsg> GetLcdMsg(string lcdName)
        {
            if (LcdDic == null) throw new Exception($"Lcd初始化未成功");
            if (!LcdDic.ContainsKey(lcdName)) throw new Exception($"未找到对应的Lcd[{lcdName}]");

            return LcdDic[lcdName].Msgs;
        }
        public List<string> GetLcdNames() 
            => LcdDic?.Values.Select(d => d.LcdName).ToList() ?? new List<string>();

        public LcdInfoDto GetLcdInfoDto(string lcdName)
        {
            if (LcdDic.ContainsKey(lcdName))
                return LcdDic[lcdName];
            return null;
        }

        public bool SetLcdMsg(string lcdName, List<LcdMsg> msgs)
        {
            if (LcdDic.ContainsKey(lcdName))
            {
                lock (LcdDic)
                {
                    LcdDic[lcdName].Msgs = msgs;
                }
                return true;
            }

            return false;
        }

        public bool SetLcdMsg(Dictionary<string, List<LcdMsg>> dic)
        {
            var count = 0;
            foreach (var key in dic.Keys)
            {
                if (LcdDic.ContainsKey(key))
                {
                    lock (LcdDic)
                    {
                        LcdDic[key].Msgs = dic[key];
                    }
                    count++;
                }
            }

            return count == dic.Count;
        }

        public bool SetLcdMsg(List<LcdInfoDto> lcdInfos)
        {
            var count = 0;
            foreach (var lcd in lcdInfos)
            {
                var key = lcd.LcdName;
                if (LcdDic.ContainsKey(key))
                {
                    lock (LcdDic)
                    {
                        LcdDic[key].Msgs = lcd.Msgs;
                    }
                    count++;
                }
            }

            return count == lcdInfos.Count;
        }

        public bool SetLcdMsg(string lcdName, Dictionary<string, string> msgs)
        {
            throw new NotImplementedException();
        }
    }
}
